<template>
    <div class="app-container">
        <!-- 总体操作按钮 -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['hotels:hotel:add']">新增</el-button>
            </el-col>

        </el-row>
        <!-- 房间类型表格数据 -->
        <el-table v-loading="loading" :data="roomTypeList">
            <el-table-column label="房间类型" align="center" prop="name" />
            <el-table-column label="可住人数" align="center" prop="capacity"></el-table-column>
            <el-table-column label="剩余房间数" align="center" prop="number"></el-table-column>
            <el-table-column label="价格" align="center" prop="price"></el-table-column>
            <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['hotels:hotel:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['hotels:hotel:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改房间类型对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <!-- 添加数据表单，可添加rules 校验规则 -->
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="房间类型" prop="name">
                    <el-input v-model="form.name" placeholder="请输入房间类型" />
                </el-form-item>
                <el-form-item label="可住人数" prop="capacity">
                    <el-input v-model="form.capacity" placeholder="请输入可住人数" />
                </el-form-item>
                <el-form-item label="剩余房间数" prop="number">
                    <el-input v-model="form.number" placeholder="请输入剩余房间数" />
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="form.price" placeholder="请输入价格" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listRoomType, addRoomType, getRoomType, delRoomType } from "@/api/hotels/roomType";/*
import { updateRoomType } from "../../../api/hotels/roomType";*/

export default {
    name: "Room",
    data() {
        return {
            // 遮罩层
            loading: true,
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 房间类型表格数据
            roomTypeList: [],
            // 对话框标题
            title: "",
            // 对话框是否显示
            open: false,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {},
        };
    },
    created() {
        // 从路由获取酒店id
        const hotelId = this.$route.params && this.$route.params.hotelId;
        // 调用方法获取数据
        this.getRoomTypeList(hotelId);
    },
    methods: {
        // 查询房间类型列表
        getRoomTypeList(hotelId) {
            this.loading = true;
            listRoomType(hotelId).then(response => {
                this.roomTypeList = response.data;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                capacity: null,
                price: null,
                hotelId: null
            };
            this.resetForm("form");
        },
        // 新增按钮操作
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加房间类型";
        },
        // 提交按钮
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRoomType(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getRoomTypeList(this.form.hotelId);
                        });
                    } else {
                        this.form.hotelId = this.$route.params.hotelId;
                        addRoomType(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getRoomTypeList(this.form.hotelId);
                        });
                    }
                }
            });
        },

        handleUpdate(row) {
            this.reset();
            const id = row.id;
            getRoomType(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改房间类型";
            });
        },
        // 删除按钮操作
        handleDelete(row) {
            const id = row.id;
            this.$modal.confirm('是否确认删除房间类型编号为"' + id + '"的数据项？').then(function () {
                return delRoomType(id);
            }).then(() => {
                this.getRoomTypeList(this.form.hotelId);
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },

    }
};
</script>
